package cn.com.zcode.user.control;

import java.util.List;

import org.apache.log4j.Logger;

import cn.com.zcode.core.control.BaseActionSupport;
import cn.com.zcode.core.util.StringUtil;
import cn.com.zcode.user.model.InviteCode;
import cn.com.zcode.user.model.User;
import cn.com.zcode.user.service.UserService;
import cn.com.zcode.user.util.UserUtil;

public class UserAction extends BaseActionSupport<UserService> {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(UserAction.class);
	private User user;
	private String inviteCode;
	private InviteCode code;
	private boolean autoLogin;
	private String url = "";

	public String login() {
		if (user == null) {
			UserUtil.removeCookieUser(this.getResponse());
			return LOGIN;
		} else {
			user = this.service.getUser(user.getUsername(), user.getPassword(), true);
			if (user != null) {
				this.service.initUser(user,this.getRequest(),this.getResponse(),autoLogin);
				return SUCCESS;
			} else {
				this.addActionError("用户名或密码错误！");
				return LOGIN;
			}
		}
	}

	public String loginOut() {
		User user = this.getSessionAttribute(User.SESSION_USER, User.class);
		this.service.destroyUser(user);
		logger.info("login out:" + user);
		return SUCCESS;
	}

	public String registInit() {
		return INPUT;
	}

	public String regist() {
		if (user == null) {
			return INPUT;
		} else {
			user.setRegistIp(UserUtil.getUserIp(this.getRequest()));
			service.addUser(user, inviteCode);
			this.addAttribute("justRegist", true);
		}
		return SUCCESS;
	}

	public void validateRegist() {
		super.validate();
		if (inviteCode == null || !service.getInviteCode(inviteCode)) {
			this.addFieldError("inviteCode", "邀请码无效");
		} else if (user != null) {
			User reUser = service.getUserByName(user.getUsername());
			if (reUser != null) {
				this.addFieldError("user.username", "用户名重复");
			}
		}
	}

	public String profile() {
		if (this.user == null) {
			User user = this.getSessionAttribute(User.SESSION_USER, User.class);
			this.user = this.service.getUserById(user.getId());
			return INPUT;
		} else {
			User ouser = this.service.getUserById(user.getId());
			ouser.setNickname(user.getNickname());
			ouser.setEmail(user.getEmail());
			String pwd = user.getPassword();
			if (pwd != null && !(pwd = pwd.trim()).isEmpty()) {
				ouser.setPassword(user.getPassword());
			}
			this.service.saveUser(ouser);
			this.addAttribute("updated", true);
			return SUCCESS;
		}
	}

	public String invite() {
		List<InviteCode> list = this.service.listInviteCodes();
		this.addAttribute("codes", list);

		return SUCCESS;
	}

	public String generInvite() {

		String inviteCode = StringUtil.getMd5Value(System.currentTimeMillis() + "");
		inviteCode = inviteCode.substring(inviteCode.length() - 6);
		User user = this.getSessionAttribute(User.SESSION_USER, User.class);
		this.code = new InviteCode();
		code.setOwner(user.getId());
		code.setEmail(this.user.getEmail());
		code.setCode(inviteCode);
		this.service.addInviteCode(this.code);

		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getInviteCode() {
		return inviteCode;
	}

	public void setInviteCode(String inviteCode) {
		this.inviteCode = inviteCode;
	}

	public InviteCode getCode() {
		return code;
	}

	public void setCode(InviteCode code) {
		this.code = code;
	}

	public boolean isAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(boolean autoLogin) {
		this.autoLogin = autoLogin;
	}

}
